var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var aboutRouter = require('./routes/about');
var teamRouter = require('./routes/team');
var structureRouter = require('./routes/structure');
var workRouter = require('./routes/work');
var personnelRouter = require('./routes/personnel');
var informationRouter = require('./routes/information');
var newsRouter = require('./routes/news');
var joinusRouter = require('./routes/joinus');
var helpRouter = require('./routes/help');
var dataRouter = require('./routes/data');
// var newspageRouter = require('./routes/newspage');



var app = express();

global.config = {
    navList:[
    	{
    		title: '首页',
			link: '/',
			index: 0
    	},
    	{
    		title: '关于千叶',
			link: '/about',
			index: 1
    	},
		{
			title: '院所结构',
			link: '/structure',
			index: 3
		},
		{
			title: '科研工作',
			link: '/work',
			index: 4
		},
		{
			title: '研究队伍',
			link: '/team',
			index: 5
		},
		{
			title: '人才培养',
			link: '/personnel',
			index: 6
		},
		{
			title: '资源信息',
			link: '/information',
			index: 7
		},
		{
			title: '新闻动态',
			link: '/news',
			index: 8
		},
		{
			title: '加入我们',
			link: '/joinus',
			index: 9
		},
		{
			title: '帮助中心',
			link: '/help',
			index: 10,
			icon: true
		}
    ],
	beian:{
		code: '粤ICP备2024239722号',
		link: 'https://beian.miit.gov.cn/'
	}
}

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/about', aboutRouter);
app.use('/team', teamRouter);
app.use('/structure', structureRouter);
app.use('/work', workRouter);
app.use('/personnel', personnelRouter);
app.use('/information', informationRouter);
app.use('/news', newsRouter);
app.use('/joinus', joinusRouter);
app.use('/help', helpRouter);
app.use('/data', dataRouter);
// app.use('/newspage', newspageRouter);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
